package com.wms.controller;

import com.wms.pojo.PageResult;
import com.wms.pojo.Result;
import com.wms.pojo.User;
import com.wms.service.UserService;
import com.wms.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Yzxxn
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //条件分页查询
    @GetMapping
    public Result queryUser(UserVo vo){
        PageResult pageResult = userService.queryUserPage(vo);
        return Result.success(pageResult);
    }

    //添加用户信息
    @PostMapping
    public Result saveUser(@RequestBody User user){
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userService.save(user);
        return Result.success();
    }

    //根据id来查询用户信息
    @GetMapping("/{id}")
    public Result queryUserById(@PathVariable Integer id){
        User user = userService.getById(id);
        return Result.success(user);
    }

    //修改用户信息
    @PutMapping
    public Result updatById(@RequestBody User user){
        user.setUpdateTime(LocalDateTime.now());
        userService.updateById(user);
        return Result.success();
    }

    //删除用户信息
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        userService.removeById(id);
        return Result.success();
    }

    //查询所有用户
    @GetMapping("/all")
    public Result getUserList(){
        List<User> list = userService.list();
        return Result.success(list);
    }
}
